package com.petshop.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.common.constant.BalzLogger;
import com.common.jdbc.JDBCConnection;
import com.petshop.model.Product;


public class ProductDaoImpl extends JDBCConnection implements ProductDao{
	
	BalzLogger logger = new BalzLogger();
	
	private static final String SQL_GET_NEW_PRODUCT = " SELECT * FROM PS_PRODUCT WHERE STATUS = 'A' ORDER BY UPDATED_DATE DESC" ;
	private static final String SQL_GET_RATE_PRODUCT = " SELECT * FROM PS_PRODUCT WHERE STATUS = 'A' AND RATE > 5  ORDER BY RATE DESC " ;
	private static final String SQL_GET_ALL_PRODUCT = " SELECT * FROM PS_PRODUCT WHERE STATUS = 'A' " ;
	
	
	public List<Product> getNewProductList() {
		
		ResultSet rs = queryBySql(SQL_GET_NEW_PRODUCT);
		List<Product> products = new ArrayList<Product>();
		
		try {
			while (rs.next()) {
				products.add(mapRow(rs));
			}
			rs.close();
		} catch (SQLException e) {
	    	logger.debug("SQLException: " + e.getMessage());
	    }
		
		if (logger.isDebugEnabled()) {
			logger.debug(" SQL_GET_NEW_PRODUCT sql: " + SQL_GET_NEW_PRODUCT);
			logger.debug("result.size(): " + products.size());
			logger.debug("result>> " + products);
		}
		
		return products;
	}
	
	@Override
	public List<Product> getRateProductList() {
		ResultSet rs = queryBySql(SQL_GET_RATE_PRODUCT);
		List<Product> products = new ArrayList<Product>();
		
		try {
			while (rs.next()) {
				products.add(mapRow(rs)); 
			}
		} catch (SQLException e) {
	    	logger.debug("SQLException: " + e.getMessage());
	    }
		
		if (logger.isDebugEnabled()) {
			logger.debug("SQL_GET_RATE_PRODUCT sql: " + SQL_GET_RATE_PRODUCT);
			logger.debug("result.size(): " + products.size());
			logger.debug("result>> " + products);
		}
		
		return products;
	}
	
	@Override
	public List<Product> getAllProductList() {
		ResultSet rs = queryBySql(SQL_GET_ALL_PRODUCT);
		List<Product> products = new ArrayList<Product>();
		
		try {
			while (rs.next()) {
				products.add(mapRow(rs)); 
			}
		} catch (SQLException e) {
	    	logger.debug("SQLException: " + e.getMessage());
	    }
		
		if (logger.isDebugEnabled()) {
			logger.debug("SQL_GET_ALL_PRODUCT sql : " + SQL_GET_ALL_PRODUCT);
			logger.debug("result.size(): " + products.size());
			logger.debug("result>> " + products);
		}
		
		return products;
	}


	
	// ---------------------------------------------------------------------------------------------------- RowMapper
	public Product mapRow(ResultSet rs) throws SQLException {
		
		Product model = new Product();
		
		model.setProductId(rs.getString("PRODUCT_ID"));
		model.setProductCode(rs.getString("PRODUCT_CODE"));
		model.setProductName(rs.getString("PRODUCT_NAME"));
		model.setProductPrice(rs.getBigDecimal("PRODUCT_PRICE"));
		model.setProductImage(rs.getString("PRODUCT_IMAGE"));
		model.setProductDesc(rs.getString("PRODUCT_DESC"));
		model.setProductStock(rs.getInt("PRODUCT_STOCK"));
		model.setStatus(rs.getString("STATUS"));
		
		return model;
	}


	
}
